
package com.putao.album.main.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.putao.album.R;
import com.putao.album.main.bean.RequestSideMoreInfo;
import com.putao.album.util.Loger;

/**
 * Created by yanglun on 15/4/29.
 */
public class FamilyUserItemView extends LinearLayout {
    public final static int FATHER = 0;
    public final static int MATHER = 1;
    public final static int WAIGONG = 2;
    public final static int WAIPO = 3;
    public final static int YEYE = 4;
    public final static int NAINAI = 5;
    public final static int CUSTOM_RELATION = 6;
    public final static int ADD_CUSTOM = 7;
    private int curType = 0;
    private ImageView family_user_icon_iv;
    private TextView family_user_name_tv;
    private boolean bRelationShip = false;

    public FamilyUserItemView(Context context) {
        super(context);
        init();
    }

    public FamilyUserItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void setRelationShip(boolean bRelationShip) {
        this.bRelationShip = bRelationShip;
    }

    public static int getTypeByTag(String tag) {
        Loger.i("tag:" + tag);
        if (tag.equals("FATHER")) {
            return FATHER;
        } else if (tag.equals("MATHER")) {
            return MATHER;
        } else if (tag.equals("WAIGONG")) {
            return WAIGONG;
        } else if (tag.equals("WAIPO")) {
            return WAIPO;
        } else if (tag.equals("YEYE")) {
            return YEYE;
        } else if (tag.equals("NAINAI")) {
            return NAINAI;
        } else if (tag.equals("")) {
            return CUSTOM_RELATION;
        } else {
            return ADD_CUSTOM;
        }
    }

    public void setFamilyUserName(String userName) {
        family_user_name_tv.setText(userName);
    }

    public int getCurType() {
        return curType;
    }


    public void setCurrentInfo(RequestSideMoreInfo.SideMoreInfo.RelationshipInfo info) {


        this.curType = getTypeByTag(info.tag);
        switch (curType) {
            case FATHER: {
                Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.baby_icon_dad);
                if (bRelationShip == false) {
                    bmp = toGrayscale(bmp);
//                    bmp = test(bmp);
                }
                family_user_icon_iv.setImageBitmap(bmp);
                family_user_name_tv.setText("爸爸");
            }
            break;
            case MATHER: {
                Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.baby_icon_mom);
                if (bRelationShip == false) {
                    bmp = toGrayscale(bmp);
//                    bmp = test(bmp);
                }
                family_user_icon_iv.setImageBitmap(bmp);
                family_user_name_tv.setText("妈妈");
            }
            break;
            case WAIGONG:
                family_user_icon_iv.setImageResource(R.drawable.baby_icon_grandfather);
                family_user_name_tv.setText("外公");
                break;
            case WAIPO:
                family_user_icon_iv.setImageResource(R.drawable.baby_icon_grandmother);
                family_user_name_tv.setText("外婆");
                break;
            case YEYE:
                family_user_icon_iv.setImageResource(R.drawable.baby_icon_grandpa);
                family_user_name_tv.setText("爷爷");
                break;
            case NAINAI:
                family_user_icon_iv.setImageResource(R.drawable.baby_icon_grandma);
                family_user_name_tv.setText("奶奶");
                break;
            case CUSTOM_RELATION:
                family_user_icon_iv.setImageResource(R.drawable.baby_icon_other);
                family_user_name_tv.setText(info.name);
                break;

            case ADD_CUSTOM:
                family_user_icon_iv.setImageResource(R.drawable.baby_icon_add);
                family_user_name_tv.setText("邀请家人");
                break;
        }
    }


    public void setCurrentType(int curType) {
        this.curType = curType;
        switch (curType) {
            case FATHER: {
                Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.baby_icon_dad);
                if (bRelationShip == false) {


                    bmp = makeTransparent(bmp, 120);

                }
                family_user_icon_iv.setImageBitmap(bmp);
                family_user_name_tv.setText("爸爸");
            }
            break;
            case MATHER: {
                Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.baby_icon_mom);
                if (bRelationShip == false) {
                    bmp = makeTransparent(bmp, 120);

                }
                family_user_icon_iv.setImageBitmap(bmp);
                family_user_name_tv.setText("妈妈");
            }
            break;
            case WAIGONG:
                family_user_icon_iv.setImageResource(R.drawable.baby_icon_grandfather);
                family_user_name_tv.setText("外公");
                break;
            case WAIPO:
                family_user_icon_iv.setImageResource(R.drawable.baby_icon_grandmother);
                family_user_name_tv.setText("外婆");
                break;
            case YEYE:
                family_user_icon_iv.setImageResource(R.drawable.baby_icon_grandpa);
                family_user_name_tv.setText("爷爷");
                break;
            case NAINAI:
                family_user_icon_iv.setImageResource(R.drawable.baby_icon_grandma);
                family_user_name_tv.setText("奶奶");
                break;
            case CUSTOM_RELATION:
                family_user_icon_iv.setImageResource(R.drawable.baby_icon_other);
                break;

            case ADD_CUSTOM:
                family_user_icon_iv.setImageResource(R.drawable.baby_icon_add);
                family_user_name_tv.setText("邀请家人");
                break;
        }
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_family_user_item_view, this);
        family_user_icon_iv = (ImageView) this.findViewById(R.id.family_user_icon_iv);
        family_user_name_tv = (TextView) this.findViewById(R.id.family_user_name_tv);
    }

    public Bitmap toGrayscale(Bitmap bmpOriginal) {
        int width, height;
        height = bmpOriginal.getHeight();
        width = bmpOriginal.getWidth();
        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Canvas c = new Canvas(bmpGrayscale);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);

        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        c.drawBitmap(bmpOriginal, 0, 0, paint);
        return bmpGrayscale;
    }


    public Bitmap makeTransparent(Bitmap src, int value) {
        int width = src.getWidth();
        int height = src.getHeight();
        Bitmap transBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(transBitmap);
        final Paint paint = new Paint();
        canvas.drawBitmap(src, 0, 0, paint);
        paint.setColor(Color.rgb(255, 255, 255));
        paint.setAlpha(value);
        canvas.drawRect(0, 0, width, height, paint);
        return transBitmap;
    }
}
